﻿
@implements IDisposable

<div class="bxjg-blazor-log-head">
    <label class="msg@(LogLevel.Debug.ToString())">
        <input type="checkbox" @bind="logDebug" />Debug
    </label>
    <label class="msg@(LogLevel.Information.ToString())">
        <input type="checkbox" @bind="logInfo" />Info
    </label>
    <label class="msg@(LogLevel.Warning.ToString())">
        <input type="checkbox" @bind="logWarn" />Warn
    </label>
    <label class="msg@(LogLevel.Error.ToString())">
        <input type="checkbox" @bind="logError" />Error
    </label>


    <input @bind="ks" type="datetime" placeholder="开始时间" />
    <input @bind="js" type="datetime" placeholder="结束时间" />

    <input @bind="kw" placeholder="关键字" />

    <button @onclick="cz">重置</button>
</div>
<div class="bxjg-blazor-log-container">
    <Virtualize TItem="KeyValuePair<int, LogMsg>" Items="Msgs" Context="item" OverscanCount="20">
        <p @key="item.Key" class="msg@(item.Value.Level.ToString())">
            <br />
            @item.Value.Time.ToString("HH:mm:ss fff")
            @item.Value.Level.ToString()
            @item.Value.Cls <br />
            @((MarkupString) item.Value.Message)<br />
            --------------------------------------------------------------
            @item.Value.ScopeState
            <br />
        </p>
    </Virtualize>
</div>

@code {
    string kw = string.Empty;

    DateTime ks = DateTime.Now.AddYears(-1), js = DateTime.Now;

    bool logDebug = true, logInfo = true, logWarn = true, logError = true;
    //LogLevel>
    //string getcls(int id) {
    //    if (LogMap.TryGetValue(id, out var str))
    //        return str;
    //        return default;
    //}

    void cz()
    {
        ks = DateTime.Now.AddYears(-1);
        js = DateTime.Now.AddYears(1);
        logDebug = true;
        logInfo = true;
        logWarn = true;
        logError = true;
        kw = string.Empty;
    }
    public List<LogLevel> filter
    {
        get
        {
            List<LogLevel> logLevels = new List<LogLevel>();
            if (logDebug)
                logLevels.Add(LogLevel.Debug);
            if (logInfo)
                logLevels.Add(LogLevel.Information);
            if (logWarn)
                logLevels.Add(LogLevel.Warning);
            if (logError)
                logLevels.Add(LogLevel.Error);
            return logLevels;
        }
    }
    public ICollection<KeyValuePair<int, LogMsg>> Msgs => BlazorServerLoggerExt.MsgContainer.Where(c => filter.Contains(c.Value.Level) && (c.Value.Message.Contains(kw) || c.Value.Cls.Contains(kw) || (c.Value.ScopeState != default && c.Value.ScopeState.ToString().Contains(kw))) && c.Value.Time >= ks && c.Value.Time < js).OrderByDescending(c => c.Value.Time).ToList();
    //[Parameter]
    //public Dictionary<int, string> LogMap { get; set; } = new Dictionary<int, string>();

    protected override void OnInitialized()
    {
        cz();
        BlazorServerLoggerExt.OnLog += Log;
    }

    public void Dispose()
    {
        BlazorServerLoggerExt.OnLog -= Log;
    }

    void Log(LogMsg msg)
    {
        this.InvokeAsync(this.StateHasChanged);
    }
}
